package com.btn.scheduler;

import com.btn.service.StudentService;
import com.btn.service.impl.StudentServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时器
 * 在pom.xml中添加所需要的的依赖
 * 在所需要的类前添加@Component
 * 在主类中添加@EnableScheduling即可开启定时器
 */
@Component
public class StudentScheduler {

    private static final Logger LOG = LoggerFactory.getLogger(StudentScheduler.class);

    @Autowired
    private StudentService studentService;

    @Scheduled(cron = "0 */10 * * * ?")
    public void scheduled() {
        LOG.info("--------------开始执行下一批数据--------------");
        try{
            Long nowTime = System.currentTimeMillis();
            studentService.saveAllStudent();
            Long endTime = System.currentTimeMillis();
            Long consumptionTime = endTime -  nowTime;
            LOG.info("最新的一批数据全部执行完毕，共执行了：" + consumptionTime / 1000 + "秒" );
        }catch (Exception e){
            LOG.warn("错误信息，生成失败:" + e + "当前时间是："  + StudentServiceImpl.getNowTime());
        }


    }
}
